<#assign fields=pojo.getFilterdPojoFields()>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>
<#assign declarationNameFirstLetterLower = pojo.importType(pojo.getDeclarationNameFirstLetterLower())>
<#assign mainDeclarationName = mainpojo.importType(mainpojo.getDeclarationName())>
<#assign mainDeclarationNameFirstLetterLower = mainpojo.importType(mainpojo.getDeclarationNameFirstLetterLower())>
<#assign realPackageAndClassPath = "${mainpojo.getRealPackageAndClassPath()}">
<#assign masterid = mainpojo.getDeclarationNameFirstLetterLower()+"Id">
import { save${declarationName}, delete${declarationName}, get${declarationName}ById, find${declarationName}ByList, find${declarationName}ByPage } from '../services/${mainDeclarationName}Service'
import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from '../../utils/config'
import modelExtend from 'dva-model-extend'
import { pageModel } from '../../models/common'
import pathToRegexp from "path-to-regexp";

export default modelExtend(pageModel, {

  namespace: '${mainDeclarationNameFirstLetterLower}${declarationName}',

  state: {
    ${mainDeclarationNameFirstLetterLower}:{},
    currentItem: {},
    selectedRowKeys:[],
    selectedRows:[],
    modalVisible: false,
    modalType: 'create',
    expand:false,
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(() => {
        const match = pathToRegexp('/${mainDeclarationNameFirstLetterLower}/${declarationNameFirstLetterLower}/:id').exec(location.pathname)
        if (match) {
          dispatch({type: 'query', payload: {${mainDeclarationNameFirstLetterLower}Id: match[1]}})
        }
      })
    },
  },

  effects: {
    *query ({ payload = {} }, { call, put }) {
      let {pageIndex, pageSize} = payload;
      pageIndex=(pageIndex == null ? 1 : pageIndex);
      pageSize = (pageSize == null ? config.defaultPageSize : pageSize);
      let query = {...payload, pageIndex, pageSize};
      const data = yield call(find${declarationName}ByPage, parse(query))
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.data.result,
            pagination: {
              current: Number(payload.pageIndex) || 1,
              pageSize: Number(payload.pageSize) || config.defaultPageSize,
              total: data.data.total,
            },
          },
        })
      }
    },

    *'delete' ({ payload }, { call, put, select }) {
      const data = yield call(delete${declarationName}, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.${declarationNameFirstLetterLower})
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },

    *'multiDelete' ({ payload }, { call, put }) {
      const data = yield call(delete${declarationName}, payload)
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },

    *create ({ payload }, { call, put }) {
      const data = yield call(save${declarationName}, payload)
      if (data.success) {
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },

    *update ({ payload }, { select, call, put }) {
      const id = yield select(({ ${declarationNameFirstLetterLower} }) => ${declarationNameFirstLetterLower}.currentItem.id)
      const new${declarationName} = { ...payload, id }
      const data = yield call(save${declarationName}, new${declarationName})
      if (data.success) {
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },

  },

  reducers: {
    showModal (state, {payload}) {
      // console.log('showModal')
      // const { modalType, currentItem } = action.payload
      // console.log(currentItem.id)
      return {...state, ...payload, modalVisible: true}
    },
    hideModal (state) {
      return { ...state, modalVisible: false }
    },
    switchExpand (state) {
      return { ...state, expand: !state.expand }
    },
  }

})
